package mse.tx.arch.pipes;

import java.io.Serializable;
import java.util.Date;

/**
 * This is the data structure that holds the data stream
 * The time is stored in long (millisecond since the Epoch)
 * but getTimestampDt() will convert and return in Date object.
 * @author ysahn
 *
 */
public class DataModel implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 397187603693397923L;
	
	public static final int BINARY_STREAM_SIZE = 72;

	protected int measurementId;
	protected long timestamp;
	protected double velocity;
	protected double altitude;
	protected double pressure;
	protected double temperature;
	protected double attitude;
	public int getMeasurementId() {
		return measurementId;
	}
	public void setMeasurementId(int measurementId) {
		this.measurementId = measurementId;
	}
	public long getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	public Date getTimestampDt()
	{
		return new Date(timestamp);
	}
	public double getVelocity() {
		return velocity;
	}
	public void setVelocity(double velocity) {
		this.velocity = velocity;
	}
	public double getAltitude() {
		return altitude;
	}
	public void setAltitude(double altitude) {
		this.altitude = altitude;
	}
	public double getPressure() {
		return pressure;
	}
	public void setPressure(double pressure) {
		this.pressure = pressure;
	}
	public double getTemperature() {
		return temperature;
	}
	public void setTemperature(double temperature) {
		this.temperature = temperature;
	}
	public double getAttitude() {
		return attitude;
	}
	public void setAttitude(double attitude) {
		this.attitude = attitude;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public static DataModel createFromBinary(byte[] dataBuffer)
	{
		int IdLength = 4;
		int MeasurementLength = 8;
		
		int cursor = 0;
		DataModel retval = new DataModel();

		int id = 0;
		long measurement = 0;

		while (cursor < dataBuffer.length) {
			
			// get id
			id = 0;
			int i = 0;
			for (i=0; i<IdLength; i++ )
			{
				id = id | (dataBuffer[cursor] & 0xFF);		// We append the byte on to ID...
	
				if (i != IdLength-1)				// If this is not the last byte, then slide the
				{									// previously appended byte to the left by one byte
					id = id << 8;					// to make room for the next byte we append to the ID
				} // if
	
				cursor++;						// Increment the byte count
	
			} // for
	
	
			// get measurement
			measurement = 0;
			for (i=0; i<MeasurementLength; i++ )
			{
				measurement = measurement | (dataBuffer[cursor] & 0xFF);	// We append the byte on to measurement...
	
				if (i != MeasurementLength-1)					// If this is not the last byte, then slide the
				{												// previously appended byte to the left by one byte
					measurement = measurement << 8;				// to make room for the next byte we append to the
																// measurement
				} // if
	
				cursor++;									// Increment the byte count
	
			} // if
			

			switch (id) {
			case 0: 
				retval.setTimestamp(measurement);
				break;
			case 1:
				retval.setVelocity(Double.longBitsToDouble(measurement));
				break;
			case 2:
				retval.setAltitude(Double.longBitsToDouble(measurement));
				break;
			case 3:
				retval.setPressure(Double.longBitsToDouble(measurement));
				break;
			case 4:
				retval.setTemperature(Double.longBitsToDouble(measurement));
				break;
			case 5:
				retval.setAttitude(Double.longBitsToDouble(measurement));
				break;
			}
			
		}
		
		return retval;
		
	}
	
	
	
}
